package com.chainshop.sale.service.impl;

import com.chainshop.common.core.domain.pojo.RevenueRecords;
import com.chainshop.common.core.domain.pojo.Sales;
import com.chainshop.sale.mapper.SaleMapper;
import com.chainshop.sale.service.ISaleService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.chainshop.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author Yeah
 * @create 2024-08-15-18:50
 * @title
 * @Description:
 */
@Service
public class SaleServiceImpl implements ISaleService {
    @Autowired
    private SaleMapper salesMapper;

    /**
     * 查询存储销售记录的
     *
     * @param orderId 存储销售记录的主键
     * @return 存储销售记录的
     */
    @Override
    public Sales selectSalesByOrderId(Integer orderId)
    {
        return salesMapper.selectSalesByOrderId(orderId);
    }

    /**
     * 查询存储销售记录的列表
     *
     * @param sales 存储销售记录的
     * @return 存储销售记录的
     */
    @Override
    public List<Sales> selectSalesList(Sales sales)
    {
        return salesMapper.selectSalesList(sales);
    }

    /**
     * 新增存储销售记录的
     *
     * @param sales 存储销售记录的
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSales(Sales sales)
    {
        int rows = salesMapper.insertSales(sales);
        insertRevenueRecords(sales);
        return rows;
    }

    /**
     * 修改存储销售记录的
     *
     * @param sales 存储销售记录的
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSales(Sales sales)
    {
        salesMapper.deleteRevenueRecordsBySalesOrderId(sales.getOrderId());
        insertRevenueRecords(sales);
        return salesMapper.updateSales(sales);
    }

    /**
     * 批量删除存储销售记录的
     *
     * @param orderIds 需要删除的存储销售记录的主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesByOrderIds(Integer[] orderIds)
    {
        salesMapper.deleteRevenueRecordsBySalesOrderIds(orderIds);
        return salesMapper.deleteSalesByOrderIds(orderIds);
    }

    /**
     * 删除存储销售记录的信息
     *
     * @param orderId 存储销售记录的主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSalesByOrderId(Integer orderId)
    {
        salesMapper.deleteRevenueRecordsBySalesOrderId(orderId);
        return salesMapper.deleteSalesByOrderId(orderId);
    }

    /**
     * 新增收入查询信息
     *
     * @param sales 存储销售记录的对象
     */
    public void insertRevenueRecords(Sales sales)
    {
        List<RevenueRecords> revenueRecordsList = sales.getRevenueRecordsList();
        Integer orderId = sales.getOrderId();
        if (StringUtils.isNotNull(revenueRecordsList))
        {
            List<RevenueRecords> list = new ArrayList<RevenueRecords>();
            for (RevenueRecords revenueRecords : revenueRecordsList)
            {
                revenueRecords.setSalesOrderId(orderId);
                list.add(revenueRecords);
            }
            if (list.size() > 0)
            {
                salesMapper.batchRevenueRecords(list);
            }
        }
    }

}
